package zdc.ui {


import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;


/**
 * 简单的刻度盘
 * @author Simple Slime
 */
public class SimpleDial extends MovieClip {
    public static const HORIZONTAL:String = "h";
    public static const VERTICAL:String = "v";

    private var cursor:DisplayObject;
    private var cursName:String = "cursorMc";
    private var max:Number;
    private var min:Number = 0;
    private var maxLen:Number;
    private var curNum:Number = 0;
    private var dir:String = "h";
    private var cursReg:Point = new Point();

    public function SimpleDial() {
        initPreVars();
        stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        initPostVars();
        initStates();
        initEvents();
        run();
    }

    private function initPreVars():void {
        cursor = this.getChildByName("cursorMc");
        maxLen = dir == "h" ? this.width : this.height;
    }

    private function initPostVars():void {

    }

    private function initStates():void {

    }

    private function initEvents():void {

    }

    private function run():void {

    }

    /**
     * 初始化功能
     */
    public function initFunction():void {

    }

    public function move(num:Number):void {
        curNum = num;
        calcPos();
    }

    private function calcPos():void {
        if (!cursor) {
            return;
        }
        var dist:Number;
        if (dir == "h") {
            dist = (maxLen - (cursor.width - cursReg.x)) * (curNum / maxNumber);
            if (dist > (maxLen - (cursor.width - cursReg.x) || dist < cursReg.x)) {

            }
            else {
                cursor.x = dist;
            }
        }
        else if (dir == "v") {
            dist = (maxLen - (cursor.height - cursReg.y)) * (curNum / maxNumber);

            if (dist > (maxLen - (cursor.height - cursReg.y)) || dist < cursReg.y) {
                //trace(dist > (maxLen - (cursor.height - cursReg.y)),  dist < cursReg.y, maxLen, cursReg.y, cursor.height, (cursor.height - cursReg.y));
            }
            else {
                cursor.y = dist;
            }
        }
        //trace(curNum, maxNumber, maxLen);
    }


    /**
     * 最大刻度数
     */
    public function get maxNumber():Number {
        return max;
    }

    public function set maxNumber(value:Number):void {
        this.max = value;
    }

    /**
     * 最小刻度数
     */
    public function get minNumber():Number {
        return this.min;
    }

    public function set minNumber(value:Number):void {
        this.min = value;
    }

    /**
     * 游标的名字
     */
    public function get cursorName():String {
        return cursName;
    }

    public function set cursorName(value:String):void {
        cursName = value;
        cursor = this.getChildByName(value);
    }

    /**
     * 刻度盘方向
     */
    public function get direction():String {
        return dir;
    }

    public function set direction(value:String):void {
        dir = value;
    }

    public function get maxLength():Number {
        return maxLen;
    }

    public function set maxLength(value:Number):void {
        maxLen = value;
    }

    public function get cursorRegister():Point {
        return cursReg;
    }

    public function set cursorRegister(value:Point):void {
        cursReg = value;
    }
}
}